package cn.wolfcode.web.service.impl;

import cn.wolfcode.web.domain.Department;
import cn.wolfcode.web.mapper.DepartmentMapper;
import cn.wolfcode.web.page.PageResult;
import cn.wolfcode.web.query.QueryObject;
import cn.wolfcode.web.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Department department) {
        departmentMapper.insert(department);
    }

    @Override
    public Department get(Long id) {
        Department department = departmentMapper.selectByPrimaryKey(id);
        return department;
    }

    @Override
    public List<Department> listAll() {

        List<Department> departments = departmentMapper.selectAll();
        return departments;
    }

    @Override
    public void update(Department department) {
        departmentMapper.updateByPrimaryKey(department);
    }

    @Override
    public PageResult query(QueryObject qo) {

        int rows = departmentMapper.queryForCount(qo);
        if (rows==0) {
            //        (int currentPage,int  pageSize,int  rows,List<?>  result)
            return  new PageResult(qo.getCurrentPage(),qo.getPageSize(),0, Collections.emptyList());
        }

        List<Department> departmentList = departmentMapper.queryForList(qo);
        return  new PageResult(qo.getCurrentPage(),qo.getPageSize(),rows,departmentList);
    }

}
